home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / program / nasm095.zip / RDOFF / RDFLIB.C < prev    next >
C/C++ Source or Header  |  1997-07-27  |  5KB  |  236 lines

  1. /* rdflib - manipulate RDOFF library files (.rdl) */
  2.  
  3. /* an rdoff library is simply a sequence of RDOFF object files, each
  4.    preceded by the name of the module, an ASCII string of up to 255
  5.    characters, terminated by a zero.  There may be an optional
  6.    directory placed on the end of the file. The format of the
  7.    directory will be 'RDL' followed by a version number, followed by
  8.    the length of the directory, and then the directory, the format of
  9.    which has not yet been designed. */
  10.  
  11. #include <stdio.h>
  12. #include <errno.h>
  13. #include <string.h>
  14.  
  15. /* functions supported:
  16.      create a library    (no extra operands required)
  17.      add a module from a library (requires filename and name to give mod.)
  18.      remove a module from a library (requires given name)
  19.      extract a module from the library (requires given name and filename)
  20.      list modules */
  21.  
  22. const char *usage = 
  23.    "usage:\n"
  24.    "  rdflib x libname [extra operands]\n\n"
  25.    "  where x is one of:\n"
  26.    "    c - create library\n"
  27.    "    a - add module (operands = filename module-name)\n"
  28.    "    r - remove                (module-name)\n"
  29.    "    x - extract               (module-name filename)\n"
  30.    "    t - list\n";
  31.  
  32. char **_argv;
  33.  
  34. #define _ENDIANNESS 0        /* 0 for little, 1 for big */
  35.  
  36. static void longtolocal(long * l)
  37. {
  38. #if _ENDIANNESS
  39.     unsigned char t;
  40.     unsigned char * p = (unsigned char *) l;
  41.  
  42.     t = p[0];
  43.     p[0] = p[3];
  44.     p[3] = t;
  45.     t = p[1];
  46.     p[1] = p[2];
  47.     p[2] = p[1];
  48. #endif
  49. }
  50.  
  51. void copybytes(FILE *fp, FILE *fp2, int n)
  52. {
  53.     int i,t;
  54.  
  55.     for (i = 0 ; i < n; i++ )
  56.     {
  57.     t = fgetc(fp);
  58.     if (t == EOF)
  59.     {
  60.         fprintf(stderr,"ldrdf: premature end of file in '%s'\n",
  61.             _argv[2]);
  62.         exit(1);
  63.     }
  64.     if (fp2) 
  65.         if (fputc(t, fp2) == EOF)
  66.         {
  67.         fprintf(stderr,"ldrdf: write error\n");
  68.         exit(1);
  69.         }
  70.     }
  71. }
  72.  
  73. long copylong(FILE *fp, FILE *fp2)
  74. {
  75.     long l;
  76.     int i,t;
  77.     unsigned char * p = (unsigned char *) &l;
  78.  
  79.  
  80.     for (i = 0 ; i < 4; i++ )    /* skip magic no */
  81.     {
  82.     t = fgetc(fp);
  83.     if (t == EOF)
  84.     {
  85.         fprintf(stderr,"ldrdf: premature end of file in '%s'\n",
  86.             _argv[2]);
  87.         exit(1);
  88.     }
  89.     if (fp2) 
  90.         if (fputc(t, fp2) == EOF)
  91.         {
  92.         fprintf(stderr,"ldrdf: write error\n");
  93.         exit(1);
  94.         }
  95.     *p++ = t;
  96.     }
  97.     longtolocal (&l);
  98.     return l;
  99. }
  100.  
  101. int main(int argc, char **argv)
  102. {
  103.     FILE *fp, *fp2;
  104.     char *p, buf[256];
  105.     int i;
  106.  
  107.     _argv = argv;
  108.  
  109.     if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3))
  110.     {
  111.     printf(usage);
  112.     exit(1);
  113.     }
  114.  
  115.     switch(argv[1][0])
  116.     {
  117.     case 'c':        /* create library */
  118.     fp = fopen(argv[2],"wb");
  119.     if (! fp) {
  120.         fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
  121.         perror("ldrdf");
  122.         exit(1);
  123.     }
  124.     fclose(fp);
  125.     break;
  126.  
  127.     case 'a':        /* add module */
  128.     if (argc < 5) {
  129.         fprintf(stderr,"ldrdf: required parameter missing\n");
  130.         exit(1);
  131.     }
  132.     fp = fopen(argv[2],"ab");
  133.     if (! fp)
  134.     {
  135.         fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
  136.         perror("ldrdf");
  137.         exit(1);
  138.     }
  139.     
  140.     fp2 = fopen(argv[3],"rb");
  141.     if (! fp)
  142.     {
  143.         fprintf(stderr,"ldrdf: could not open '%s'\n",argv[3]);
  144.         perror("ldrdf");
  145.         exit(1);
  146.     }
  147.  
  148.     p = argv[4];
  149.     do {
  150.         if ( fputc(*p,fp) == EOF ) {
  151.         fprintf(stderr,"ldrdf: write error\n");
  152.         exit(1);
  153.         }
  154.     } while (*p++);
  155.  
  156.     while (! feof (fp2) ) {
  157.         i = fgetc (fp2);
  158.         if (i == EOF) {
  159.         break;
  160.         }
  161.  
  162.         if ( fputc(i, fp) == EOF ) {
  163.         fprintf(stderr,"ldrdf: write error\n");
  164.         exit(1);
  165.         }
  166.     }
  167.     fclose(fp2);
  168.     fclose(fp);
  169.     break;
  170.  
  171.     case 'x':
  172.     if (argc < 5) {
  173.         fprintf(stderr,"ldrdf: required parameter missing\n");
  174.         exit(1);
  175.     }
  176.  
  177.     fp = fopen(argv[2],"rb");
  178.     if (! fp)
  179.     {
  180.         fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]);
  181.         perror("ldrdf");
  182.         exit(1);
  183.     }
  184.     
  185.     fp2 = NULL;
  186.     while (! feof(fp) ) {
  187.         /* read name */
  188.         p = buf;
  189.         while( ( *(p++) = (char) fgetc(fp) ) )  
  190.         if (feof(fp)) break;
  191.  
  192.         if (feof(fp)) break;
  193.  
  194.         /* check against desired name */
  195.         if (! strcmp(buf,argv[3]) )
  196.         {
  197.         fp2 = fopen(argv[4],"wb");
  198.         if (! fp2)
  199.         {
  200.             fprintf(stderr,"ldrdf: could not open '%s'\n", argv[4]);
  201.             perror("ldrdf");
  202.             exit(1);
  203.         }
  204.         }
  205.         else
  206.         fp2 = NULL;
  207.  
  208.         /* step over the RDOFF file, copying it if fp2 != NULL */
  209.         copybytes(fp,fp2,6);    /* magic number */
  210.         copybytes(fp,fp2, copylong(fp,fp2));    /* header */
  211.         copybytes(fp,fp2, copylong(fp,fp2));    /* text */
  212.         copybytes(fp,fp2, copylong(fp,fp2));    /* data */
  213.  
  214.         if (fp2)
  215.         break;
  216.     }
  217.     fclose(fp);
  218.     if (fp2)
  219.         fclose(fp2);
  220.     else
  221.     {
  222.         fprintf(stderr,"ldrdf: module '%s' not found in '%s'\n",
  223.             argv[3],argv[2]);
  224.         exit(1);
  225.     }
  226.     break;
  227.  
  228.     default:
  229.     fprintf(stderr,"ldrdf: command '%c' not recognised\n",
  230.         argv[1][0]);
  231.     exit(1);
  232.     }
  233.     return 0;
  234. }
  235.  
  236.